package com.baseframe.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baseframe.common.annotation.DataFilter;
import com.baseframe.common.utils.PageUtils;
import com.baseframe.sys.entity.FrameRole;
import com.baseframe.sys.service.IFrameRoleService;
import com.baseframe.sys.service.dao.FrameRoleRepository;


/**
 * 角色
 * 
 * @author xuxf
 */
@Service("frameRoleService")
public class FrameRoleServiceImpl  implements IFrameRoleService {
//	@Autowired
//	private SysRoleMenuService sysRoleMenuService;
//	@Autowired
//	private SysRoleDeptService sysRoleDeptService;
//	@Autowired
//	private SysUserRoleService sysUserRoleService;
//	@Autowired
//	private SysDeptService sysDeptService;
	
	@Autowired
	private FrameRoleRepository frameRoleRepository;
	@PersistenceContext
	private EntityManager em;

	@Override
	@DataFilter(subDept = true, user = false)
	public PageUtils queryPage(Map<String, Object> params) {
		String rolename = (String)params.get("rolename");
		int pageSize = Integer.parseInt((String)params.get("limit"));
		int page = Integer.parseInt((String)params.get("page"));
		String sidx = (String)params.get("sidx");
		String order = (String)params.get("order");
		String chql="select count(1) from FrameRole t where 1=1";
		String hql="from FrameRole t where 1=1";
		if(rolename!=null&&!rolename.trim().equals("")){
			chql+=" and t.rolename like '%"+rolename+"%'";
			hql+=" and t.rolename like '%"+rolename+"%'";
		}
		if(sidx!=null&&!sidx.equals("")){
			hql+=" order by t."+sidx+" "+order;
		}
		int total = Integer.parseInt(em.createQuery(chql).getSingleResult().toString());
		
		List<FrameRole> userlist= em.createQuery(hql).setFirstResult((page-1)*pageSize).setMaxResults(page*pageSize).getResultList();
		
		PageUtils p =  new PageUtils(userlist, total, pageSize, page);
		return p;
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(FrameRole role) {
		role.setCreate_time(new Date());
		role.setRoleguid(UUID.randomUUID().toString().replace("-", ""));
//		this.insert(role);
		frameRoleRepository.save(role);

//		//保存角色与菜单关系
//		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
//
//		//保存角色与部门关系
//		sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
	}

//	@Override
//	@Transactional(rollbackFor = Exception.class)
//	public void update(FrameRole role) {
//		this.updateById(role);
//		
////		//更新角色与菜单关系
////		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
////
////		//保存角色与部门关系
////		sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
//	}
	
	@Override
    public void update(FrameRole role) {
		frameRoleRepository.save(role);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String[] roleIds) {
		//删除角色
//		this.deleteBatchIds(Arrays.asList(roleIds));
		for(int i=0;i<roleIds.length;i++){
			frameRoleRepository.delete(roleIds[i]);
		}
				

//		//删除角色与菜单关联
//		sysRoleMenuService.deleteBatch(roleIds);
//
//		//删除角色与部门关联
//		sysRoleDeptService.deleteBatch(roleIds);
//
//		//删除角色与用户关联
//		sysUserRoleService.deleteBatch(roleIds);
	}
	
	
	 @Override
	    public FrameRole selectById(String id) {
	        return frameRoleRepository.findOne(id);
	    }


	@Override
	public List<FrameRole> findAll() {
		// TODO Auto-generated method stub
		return frameRoleRepository.findAll();
	}


}
